package com.xms.queryview.api.controller;

import com.xms.core.exception.XmsException;
import com.xms.core.model.KeyValuePair;
import com.xms.queryview.api.model.CreateQueryViewModel;
import com.xms.queryview.entity.QueryView;
import com.xms.queryview.service.IQueryViewCreaterService;
import com.xms.schema.entity.Entity;
import com.xms.schema.service.entity.IEntityFinderService;
import com.xms.utils.CollectionUtil;
import com.xms.utils.JsonUtil;
import com.xms.utils.StringUtil;
import com.xms.utils.UUIDUtil;
import com.xms.web.framework.controller.ApiBaseController;
import com.xms.web.framework.model.JsonResultObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLException;
import java.time.LocalDateTime;

/**
 * <p>
 * QueryViewCreaterController
 * </p>
 *
 * @author migo
 * @since 2021/9/18
 */
@RestController
@RequestMapping("{org}/api/schema/queryview")
public class QueryViewCreaterController extends ApiBaseController {
    private IQueryViewCreaterService _queryViewCreaterService;
    private IEntityFinderService _entityFinderService;
    @Autowired
    public void QueryViewCreaterController(IQueryViewCreaterService queryViewCreaterService,
                                           IEntityFinderService entityFinderService) {
        _queryViewCreaterService = queryViewCreaterService;
        _entityFinderService = entityFinderService;
    }

    @ApiOperation("新建视图")
    @PostMapping("create")
    public JsonResultObject create(@RequestBody CreateQueryViewModel model) throws XmsException, SQLException {
        if(UUIDUtil.isEmpty(model.entityid)){
            return JNotSpecifiedParams("entityid");
        }
        if(StringUtil.isEmpty(model.name)){
            return JNotSpecifiedParams("name");
        }
        Entity entity = _entityFinderService.getById(model.entityid);
        if(entity == null){
            return JNotFound("实体不存在");
        }
        QueryView view = new QueryView();
        view.setQueryviewid(UUIDUtil.get());
        view.setCreatedby(_currentUser.getUser().SystemUserId);
        view.setCreatedon(LocalDateTime.now());
        view.setAuthorizationenabled(false);
        view.setIscustombutton(model.iscustombutton);
        if(model.iscustombutton || CollectionUtil.notEmpty(model.buttonid)) {
            view.setCustombuttons(JsonUtil.toJSONString(model.buttonid));
        }
        view.setDescription(model.description);
        view.setEntityid(model.entityid);
        view.setEntityname(entity.getName());
        view.setIsdefault(false);
        view.setIssimplefilter(false);
        view.setIsprivate(false);
        view.setLayoutconfig(model.layoutconfig.trim());
        view.setFetchconfig(model.fetchconfig.trim());
        view.setAggregateconfig(model.aggregateconfig.trim());
        view.setName(model.name);
        view.setStatecode(true);
        view.setTargetformid(model.targetformid);
        return _queryViewCreaterService.create(view) ?
                JCreateSuccess(KeyValuePair.build("id", view.getQueryviewid())) : JCreateFailure();
    }
}
